﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace AnnotateMe.Interfaces
{
    public class ImmutableRectangle : Shape
    {
        private readonly ImmutablePoint originPoint;
        private readonly ImmutableSize size;
        private readonly Action<ImmutableRectangle> draw;

        public ImmutableRectangle(ImmutablePoint originPoint, ImmutableSize size, Action<ImmutableRectangle> draw)
        {
            this.originPoint = originPoint;
            this.size = size;
            this.draw = draw;
        }

        public ImmutablePoint OriginPoint { get { return this.originPoint; } }
        public ImmutableSize Size { get { return this.size; } }

        public ImmutableRectangle WithOrigin(ImmutablePoint newOrigin)
        {
            return new ImmutableRectangle(newOrigin, this.Size, this.draw);
        }

        public override void Draw()
        {
            this.draw(this);
        }

        public override ImmutableRectangle GetContainingRectangle()
        {
            return this;
        }

        public override Shape MoveBy(double deltaX, double deltaY)
        {
            return this.WithOrigin(new ImmutablePoint(this.originPoint.X + deltaX, this.originPoint.Y + deltaY));
        }

        public bool IntersectsWith(ImmutableRectangle other)
        {
            var rect1 = this.ToRectangleF();
            var rect2 = other.ToRectangleF();

            return rect1.IntersectsWith(rect2);
        }

        public RectangleF ToRectangleF()
        {
            return new RectangleF((float)this.OriginPoint.X, (float)this.OriginPoint.Y, (float)this.Size.Width, (float)this.Size.Height);
        }

        public Rectangle ToRectangle()
        {
            return new Rectangle((int)this.OriginPoint.X, (int)this.OriginPoint.Y, (int)this.Size.Width, (int)this.Size.Height);
        }
    }
}
